package com.lbl.third.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lbl.third.common.R;
import com.lbl.third.enytity.Category;
import com.lbl.third.service.CateGoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 分类管理
 */
@RestController
@RequestMapping("/category")
public class CateGoryController {

    @Autowired
    private CateGoryService cateGoryService;


    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){

        cateGoryService.save(category);
        return R.success("保存成功");
    }


    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        //分页构造器
        Page<Category> page1=new Page<>(page,pageSize);

        //条件构造器
        LambdaQueryWrapper<Category> QueryWrapper=new LambdaQueryWrapper<>();

        //添加排序条件，根据Sort升序排列
        QueryWrapper.orderByAsc(Category::getSort);

        //进行分页查询
        cateGoryService.page(page1,QueryWrapper);

        return R.success(page1);
    }

    /**
     * 根据id进行删除分类
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){

        cateGoryService.remove(ids);

        return R.success("分类删除成功");
    }


    /**
     * 修改分类名称与排序
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){

        cateGoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public  R<List<Category>> list(Category category){
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();

        //添加条件
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());

        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        //执行查询操作
        List<Category> list=cateGoryService.list(queryWrapper);

        //返回查询数据
        return R.success(list);

    }

}
